######################################################
# 基本对象类型
######################################################

## 向量
# 整形：序列元素默认类型
typeof(5:5)
# 双精度类型：默认类型双精度类型
# 复数型：实部+虚部 即使实部为0  0+1i
# 字符串："Hello World"
# 逻辑值：TRUE FALSE

## 复合对象
# 列表：list(1, 2, "hat")

# 矩阵
m <- matrix(data=1:12, nrow = 4, ncol = 3, 
            dimnames = list(c("r1", "r2", "r3", "r4"),
                            c("c1", "c2", "c3")))
l <- list(1:12)
m2 <- as.matrix(l)
# 数组
a <- array(data=1:24, dim = c(3, 4, 2))
a
# 因子：用来表示分类变量
student.score <- factor(c("优", "优", "良", "差", "中", "良", "优"),
                        levels=c("差", "中", "良", "优"),
                        ordered=TRUE)
student.score
student.score.integer <- unclass(student.score)
student.score.integer
class(student.score.integer) <- "factor"
student.score.integer

gl <- gl(2,5)
#查看变量的类，显示为因子；
class(gl)
#查看数据大类，显示为数值型；
mode(gl)
#查看数据细类，显示为整数型；
typeof(gl)

# 数据框
student <- data.frame(
  name = c("胡彬", "姜静", "赵丹", "Name1", "Name2", "Name3", "Name4"),
  score = student.score
)
typeof(student)
class(student)

# 公式：表示变量之间的关系
sample.formula <- as.formula(y~x1+x2+x3)
class(sample.formula)

# 时间序列
ts(1:8, start = c(2008, 2), frequency = 4)

# 日期时间对象
as.Date("2/13/2009", "%m/%d/%Y")
today <- Sys.Date()
today

# 属性
attributes(student)
dim(student)
dimnames(student)
rownames(student)
colnames(student)

# 移除属性
dim(m) <- NULL

# 对象对比
a <- array(1:12, dim=c(3, 4))
b <- 1:12
a[2,2]
a == b
all.equal(a, b)
identical(a, b)
dim(b) <- c(3, 4)
all.equal(a, b)
identical(a, b)